/**
 * Copyright(c) 2010 Ceno Techonologies Co., Ltd.
 *
 * History:
 *   15-7-28 下午2:34 Created by lyyang
 */
package com.jade.bss.security.shiro.authc;

import javax.inject.Inject;
import javax.inject.Named;

import com.jade.bss.base.BssException;
import com.jade.bss.base.customer.Customer;
import com.jade.bss.service.sms.VerifyCodeManager;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.credential.CredentialsMatcher;

/**
 * 验证码登录
 *
 * @author <a href="mailto:lyyang@ceno.cn">lyyang</a>
 * @version 1.0 15-7-28 下午2:34
 */
public class VerifyCodeMatcher implements CredentialsMatcher
{

    private VerifyCodeManager verifyCodeManager;

    @Inject
    public void setVerifyCodeManager(@Named ("bss_verifyCodeManager")VerifyCodeManager verifyCodeManager)
    {
        this.verifyCodeManager = verifyCodeManager;
    }

    public boolean doCredentialsMatch(AuthenticationToken authenticationToken, AuthenticationInfo authenticationInfo)
    {
        String password = null;
        Object credentials = authenticationToken.getCredentials();
        String scope = ((VerifyCodeToken)authenticationToken).getScope();
        if (credentials instanceof char[]) {
            password = new String((char[]) credentials);
        }
        else {
            password = (String) credentials;
        }
        try {
            return verifyCodeManager.checkVerifyCode(scope,((Customer)(authenticationInfo.getPrincipals().getPrimaryPrincipal())).getName(),password);
        }
        catch (BssException e) {
            e.printStackTrace();
            return false;

        }
    }
}
